package task3;

import java.util.Random;

public class Consumer implements Runnable {

	private int _ID;
	private final int MAX_ATTEMPTS = 10;
	private CyclicBuffer<String> buffer;

    public Consumer(int ID, CyclicBuffer<String> buffer) {
        this._ID = ID;
    	this.buffer = buffer;
    }

    public void run() {
        Random random = new Random();
        int attemptCounter = 0;
        for (String message = buffer.retrieveString();
        		attemptCounter < MAX_ATTEMPTS; message = buffer.retrieveString()) {
        	if (!message.isEmpty()) {
        		attemptCounter = 0;
        		System.out.println("CONSUMER " + _ID + " - MESSAGE RECEIVED: " + message);
        	} else {
        		attemptCounter++;
        		System.out.println("CONSUMER " + _ID + " - BUFFER IS EMPTY. WAITING...");
        	}
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) { }
        }
    }
}